<?php
/**
 * Holds class Mvc_View_JPGraph
 *
 * @author daantimmer
 *
 * @package Library
 * @subpackage Mvc
 */

/**
 * A view to output data as a PNG Graph using the JPGraph library
 *
 * This class uses the JPGraph library to output data/statistics as a graph.
 * it works basicly the same was as smarty does.
 * It uses PHP-templates located in APP/views/graphs/$controller/$action.php
 *
 * @author daantimmer
 */
class Mvc_View_JPGraph extends Mvc_View{
	
	/**
	 * @see Mvc_View_Interface::render()
	 */
	public function render(){
		JPGraph::import("jpgraph");
		
		$controller = strtolower(substr($this->_controller,0,strlen($this->_controller) - strlen('Controller')));
		$action = strtolower(substr($this->_action,strlen('action')));
			
		if(!isset($this->_var['plotData']))
			throw new Exception("no plotData specified");
		
		$graphDir = APP . 'views' . DIRECTORY_SEPARATOR . 'graphs' . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $action . '.php';
		
		require_once($graphDir);
		
		if(!class_exists('Graph_Renderer')){
			throw new Exception("graph view does not exist");
		}
			
		$graph = new Graph_Renderer(800,400,"auto");
		JPGraph::import($graph->getFiles());

		ob_start();
		$graph->render($this->_var['plotData']);
		$graph->stroke();
		$contents =  ob_get_contents();
		ob_end_clean();
		
		return $contents;
		
		
		
		/**********************/
		/*
		$compiledir = APP . 'tpl' . DIRECTORY_SEPARATOR . strtolower($module) . DIRECTORY_SEPARATOR . 'bin';
		$cachedir= APP . 'tpl' . DIRECTORY_SEPARATOR . strtolower($module) . DIRECTORY_SEPARATOR . 'cache';
		$srcdir = APP . 'tpl' . DIRECTORY_SEPARATOR . strtolower($module) . DIRECTORY_SEPARATOR . 'src';
		
		$smarty->compile_dir = $compiledir;
		$smarty->cache_dir = $cachedir;
		$smarty->template_dir = $srcdir;
		
		$page = $controller . DIRECTORY_SEPARATOR . $action . '.html';
		$views = APP . 'views' . DIRECTORY_SEPARATOR . $module;
		
		$smarty->assign("page",$page);
		$smarty->assign("views",$views);
		
		foreach($this->_var as $key => $var)
			$smarty->assign($key,$var);
		
		return $smarty->fetch($this->_template.'.html');
		*/
	}
}